package com.masterclient.app.businessmodule.minemodule.cash;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.eoner.baselib.widget.navgation.FCNavigationBar;
import com.eoner.baselibrary.utils.StringsUtils;
import com.eoner.managerlibrary.system.SystemConfigStorage;
import com.eoner.managerlibrary.user.UserDataManager;
import com.eoner.common.config.Config;
import com.masterclient.app.R;
import com.masterclient.app.base.FCBusinessActivity;
import com.masterclient.app.businessmodule.minemodule.cash.contract.BinderPhoneContract;
import com.masterclient.app.businessmodule.minemodule.cash.contract.BinderPhonePresenter;
import com.masterclient.app.utils.CaptchaUtil;
import com.google.gson.Gson;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 作者：sean
 * 创建时间：2017/10/23 17:09
 * 功能说明：
 */

public class BinderMobileActivity extends FCBusinessActivity<BinderPhonePresenter> implements BinderPhoneContract.View {


    @BindView(R.id.login_back)
    ImageView loginBack;
    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_verify)
    TextView tvVerify;
    @BindView(R.id.et_verify_code)
    EditText etVerifyCode;
    @BindView(R.id.tv_login)
    TextView tvLogin;

    String phoneNum, verifyCode;

    CaptchaUtil captchaUtil;
    Gson gson;
    @BindView(R.id.rl_code)
    RelativeLayout rlCode;
    @BindView(R.id.view_code)
    View viewCode;

    @Override
    protected int requireGetLayoutId() {
        return R.layout.activity_bind_phone;
    }

    @Override
    protected void requireSetCustomNavigationBar(FCNavigationBar navigationBar) {
        navigationBar.setVisibility(View.GONE);
    }

    @Override
    protected void requireInitUIAndData(Bundle savedInstanceState) {
        rlCode.setVisibility(View.GONE);
        viewCode.setVisibility(View.GONE);
        gson = new Gson();
        tvVerify.setClickable(false);
        tvLogin.setClickable(false);
        etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 11) {
                    tvVerify.setClickable(true);
                    tvVerify.setBackgroundResource(R.drawable.login_bg_3);
                    tvVerify.setTextColor(0xffcc363c);
                } else {
                    tvVerify.setClickable(false);
                    tvVerify.setBackgroundResource(R.drawable.login_bg_1);
                    tvVerify.setTextColor(0xffcccccc);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        etVerifyCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() == 6) {
                    tvLogin.setClickable(true);
                    tvLogin.setBackgroundResource(R.drawable.full_round_red);
                    tvLogin.setTextColor(0xffffffff);
                } else {
                    tvLogin.setClickable(false);
                    tvLogin.setBackgroundResource(R.drawable.login_bg_2);
                    tvLogin.setTextColor(0xffffffff);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });


        captchaUtil = new CaptchaUtil(mContext);
        captchaUtil.setCaptchaListerner(new CaptchaUtil.CaptchaListerner() {
            @Override
            public void onSuccess(String ne) {
                mPresenter.getCaptcha(phoneNum, "bind_mobile", ne);
                tvVerify.setClickable(false);
            }

            @Override
            public void onFailed() {
                tvVerify.setClickable(true);
            }
        });
    }

    @OnClick({R.id.login_back, R.id.tv_verify, R.id.tv_login})
    void onClick(View v) {
        switch (v.getId()) {
            case R.id.login_back:
                finish();
                break;
            case R.id.tv_verify:
                phoneNum = etPhone.getText().toString().trim();
                if (StringsUtils.isPhoneNum(phoneNum)) {
                    if (SystemConfigStorage.getInstance().isOpenSlipPageCode()) {
                        if (CaptchaUtil.CAPTCHA_NUMBER_TIMES >= Config.CAPTCHA_MAX_TIMES){
                            mPresenter.getCaptcha(phoneNum, "bind_mobile", "");
                        }else {
                            captchaUtil.start();
                        }
                    } else {
                        mPresenter.getCaptcha(phoneNum, "bind_mobile", "");
                        tvVerify.setClickable(false);
                    }
                } else {
                    showToast("请输入正确的手机号");
                }
                break;
            case R.id.tv_login:
                phoneNum = etPhone.getText().toString().trim();
                verifyCode = etVerifyCode.getText().toString().trim();
                if (StringsUtils.isPhoneNum(phoneNum)) {
                    if (TextUtils.isEmpty(verifyCode)) {
                        showToast("验证码不能为空");
                    } else if (verifyCode.length() < 6) {
                        showToast("验证码长度不足6位");
                    } else {
                        mPresenter.bindMobile(verifyCode, phoneNum);
                    }
                } else {
                    showToast("请输入正确的手机号");
                }
                break;
        }
    }

    @Override
    protected void onPause() {
        hideSoftInput(etPhone);
        super.onPause();
    }

    @Override
    public void getCaptchaSuccess() {
        tvVerify.setBackgroundResource(R.drawable.login_bg_1);
        tvVerify.setTextColor(0xffcccccc);
        CountDownTimer timer = new CountDownTimer(60 * 1000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                tvVerify.setText(millisUntilFinished / 1000 + "s");
            }

            @Override
            public void onFinish() {
                tvVerify.setClickable(true);
                tvVerify.setBackgroundResource(R.drawable.login_bg_3);
                tvVerify.setTextColor(0xffcc363c);
                tvVerify.setText("获取验证码");

                etPhone.setFocusableInTouchMode(true);

                etPhone.setFocusable(true);

                etPhone.requestFocus();
            }
        };
        timer.start();
        etPhone.setFocusable(false);
        etPhone.setFocusableInTouchMode(false);

    }

    @Override
    public void getCaptchaFailed() {
        showToast("获取验证码失败，请重试");
        tvVerify.setClickable(true);
        tvVerify.setBackgroundResource(R.drawable.login_bg_3);
        tvVerify.setTextColor(0xffcc363c);
        tvVerify.setText("获取验证码");
    }

    @Override
    public void authBindSuccess() {
        showToast("绑定成功");
        UserDataManager.getInstance().updateUserMobile(phoneNum);
        setResult(104);
        finish();
    }

    @Override
    public void authBindFailded() {
        showToast("绑定失败");
    }

}
